<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/banner.css">
    <script src="jquery.min.js"></script>
    <!-- <script src="js/banner.js"></script> -->
    <!-- <script src="js/jq banner .js"></script> -->
    <!-- <script src="js/jqbanner.js"></script> -->
</head>

<body>
    <div class="mybanner">
        <!-- 图片 -->
        <ul class="hot">
            <li>
                <a href=""> <img src="images/banner1.jpg" alt=""> </a>
            </li>
            <li>
                <a href="#"> <img src="images/banner2.jpg" alt=""> </a>
            </li>
            <li>
                <a href="#"> <img src="images/banner3.jpg" alt=""> </a>
            </li>
            <li>
                <a href="#"> <img src="images/banner4.jpg" alt=""> </a>
            </li>
        </ul>
        <!-- 箭头 -->
        <div class='arrow'>
            <span class="prev"> < </span>
            <span class="next"> > </span>
        </div>
        <!-- 小圆点 -->

        <ul class="circle-list">
            <li class="circle active" data-n="0"></li>
            <li class="circle" data-n="1"></li>
            <li class="circle" data-n="2"></li>
            <li class="circle" data-n="3"></li>
        </ul>
    </div>
    <!-- <script>
        window.onload = function() {
            var mybanner = document.querySelector(".mybanner");
            var prev = document.querySelector('.prev');
            var next = document.querySelector('.next');

            mybanner.addEventListener('mouseenter', function() { //鼠标经过轮播图，显示左右箭头
                prev.style.display = 'block';
                next.style.display = 'block';

            })
            mybanner.addEventListener('mouseleave', function() { //鼠标离开轮播图，隐藏左右箭头
                prev.style.display = 'none';
                next.style.display = 'none';


            })
            var width = mybanner.offsetWidth; //轮播图的宽度
            var i = 0;
            var hot = document.querySelector('.hot');
            var temp = hot.firstElementChild.cloneNode(true); //复制第一张图片
            hot.appendChild(temp); //将第一张图片添加到最后
            //right arrow
            hot.imgChange = () => {
                i++;
                hot.style.transform = 'translatex(' + (-i * width) + 'px)'; //轮播图的位移
                hot.style.transition = 'all 0.5s'; //轮播图的过渡时间
                if (i == hot.children.length - 1) { //当轮播到最后一张图片时，回到第一张图片
                    i = 0; //将i重置为0
                    setTimeout(() => { //等待0.5秒，将轮播图的位移重置为0
                            hot.style.transform = 'translatex(0px)'; //将轮播图的位移重置为0
                            hot.style.transition = 'none'; //将轮播图的过渡时间重置为0
                        }, 500) //等待0.5秒
                }
                setCircle(i); //调用小圆点函数
            }

            next.onclick = hot.imgChange; //点击右箭头，调用轮播函数
            //left arrow
            console.log(hot.children.length);
            xc


            hot.imgChange2 = () => { //点击左箭头，轮播图往左移动

                if (i == 0) { //当轮播到第一张图片时，回到最后一张图片
                    i = hot.children.length - 1;
                    hot.style.transform = 'translatex(' + (-hot.children.length + 1) * width + 'px)';
                    hot.style.transition = 'none';

                }
                i--;
                setTimeout(() => { //等待0.5秒，将轮播图的位移重置为0

                    hot.style.transform = 'translatex(' + (-i * width) + 'px)';
                    hot.style.transition = 'all 0.5s';
                }, 0)
                setCircle(i);
            }

            prev.onclick = hot.imgChange2;

            //小圆点
            var setCircle = (i) => { //小圆点的函数
                var circleList = document.querySelectorAll('.circle');
                circleList.forEach(item => { //遍历小圆点
                    item.classList.remove('active');
                    var n = item.getAttribute('data-n'); //获取小圆点的data-n属性
                    if (n == i) { //
                        item.classList.add('active');
                    }

                })
            }

            
            var ul = document.querySelector('.circle-list');

            ul.onclick = (e) => {
                    if (e.target.nodeName == 'LI') {
                        i = Number(e.target.getAttribute('data-n'));
                        hot.style.transform = 'translatex(' + (-i * width) + 'px)';
                        hot.style.transition = 'all 0.5s';
                        setCircle(i);
                    }

                }
                //自动轮播
            var t = setInterval(hot.imgChange, 2000); //每2秒轮播一次 向右
            mybanner.onmouseenter = () => { //经过轮播图，停止轮播     
                clearInterval(t);
            }
            mybanner.onmouseleave = () => { //离开轮播图，继续轮播
                t = setInterval(hot.imgChange, 2000);
            }
        }
    </script> -->
    <!-- <script>
        $(window).on('load', function() {
            var $mybanner = $('.mybanner');
            var $prev = $('.prev');
            var $next = $('.next');

            $mybanner.on('mouseenter', function() { //鼠标经过轮播图，显示左右箭头
                $prev.show();
                $next.show();
            }).on('mouseleave', function() { //鼠标离开轮播图，隐藏左右箭头
                $prev.hide();
                $next.hide();
            });

            var width = $mybanner.width(); //轮播图的宽度
            var i = 0;
            var $hot = $('.hot');
            var temp = $hot.children().first().clone(); //复制第一张图片
            $hot.append(temp); //将第一张图片添加到最后

            // 右箭头点击事件
            $hot.imgChange = function() {
                i++;
                $hot.css('transform', 'translatex(' + (-i * width) + 'px)').css('transition', 'all 0.5s');
                if (i == $hot.children().length - 1) {
                    i = 0;
                    setTimeout(function() {
                        $hot.css('transform', 'translatex(0px)').css('transition', 'none');
                    }, 500);
                }
                setCircle(i);
            };
            // 右箭头点击事件 
            // $hot.imgChange = function() {
            //     i++;
            //     $hot.animate({
            //         'transform': 'translatex(' + (-i * width) + 'px)'
            //     }, 500, function() {
            //         if (i == $hot.children().length - 1) {
            //             i = 0;
            //             setTimeout(function() {
            //                 $hot.css('transform', 'translatex(0px)').css('transition', 'none');
            //             }, 500);
            //         }
            //         setCircle(i);
            //     });
            // };

            $next.on('click', $hot.imgChange);

            // 左箭头点击事件
            $hot.imgChange2 = function() {
                if (i == 0) {
                    i = $hot.children().length - 1;
                    $hot.css('transform', 'translatex(' + (-$hot.children().length + 1) * width + 'px)').css('transition', 'none');
                }
                i--;
                setTimeout(function() {
                    $hot.css('transform', 'translatex(' + (-i * width) + 'px)').css('transition', 'all 0.5s');
                }, 0);
                setCircle(i);
            };

            $prev.on('click', $hot.imgChange2);

            // 小圆点点击事件
            var setCircle = function(i) {
                $('.circle').removeClass('active');
                $('.circle[data-n="' + i + '"]').addClass('active');
            };

            $('.circle-list').on('click', 'li', function(e) {
                if (e.target.nodeName == 'LI') {
                    i = Number($(e.target).attr('data-n'));
                    $hot.css('transform', 'translatex(' + (-i * width) + 'px)').css('transition', 'all 0.5s');
                    setCircle(i);
                }
            });

            // 自动轮播
            var t = setInterval($hot.imgChange, 2000);
            $mybanner.on('mouseenter', function() {
                clearInterval(t);
            }).on('mouseleave', function() {
                t = setInterval($hot.imgChange, 2000);
            });
        });
    </script> -->
    <script>
        $(function() {
            $('.mybanner').on('mouseenter', function() { //
                $('.prev').show();
                $('.next').show();
                // clearInterval(timer);

            }).on('mouseleave', function() {
                $('.prev').hide();
                $('.next').hide();
                // clearInterval(timer);
            });

            var width = $('.mybanner').width();
            var i = 0;
            $('.hot').append($('.hot li').eq(0).clone(true));

            // $hot.imgChange = function() {
            //     i++;
            //     $hot.css('transform', 'translatex(' + (-i * width) + 'px)').css('transition', 'all 0.5s');
            //     if (i == $hot.children().length - 1) {
            //         i = 0;
            //         setTimeout(function() {
            //             $hot.css('transform', 'translatex(0px)').css('transition', 'none');
            //         }, 500);
            //     }
            //     setCircle(i);
            // };
            // 右箭头点击事件
            imgChange = function() {
                i++;
                $('.hot').css('transform', 'translatex(' + (-i * width) + 'px)').css('transition', 'all 0.5s');
                if (i == $('.hot').children().length - 1) {
                    i = 0;
                    setTimeout(function() {
                        $('.hot').css('transform', 'translatex(0px)').css('transition', 'none');
                    }, 500);
                }
                setCircle(i);
            };
            $('.next').on('click', imgChange);

            // 左箭头点击事件
            imgChange2 = function() {
                if (i == 0) {
                    i = $('.hot').children().length - 1;
                    $('.hot').css('transform', 'translatex(' + (-$('.hot').children().length + 1) * width + 'px)').css('transition', 'none');
                }
                i--;
                setTimeout(function() {
                    $('.hot').css('transform', 'translatex(' + (-i * width) + 'px)').css('transition', 'all 0.5s');
                }, 0);
                setCircle(i);
            };

            $('.prev').on('click', imgChange2);
            // 自动轮播
            var t = setInterval($('.hot').imgChange, 2000); //
            $('.mybanner').on('mouseenter', function() {
                clearInterval(t);
            }).on('mouseleave', function() {
                t = setInterval($('.hot').imgChange, 2000);
            });

            // 小圆点点击事件
            var setCircle = function(i) {
                $('.circle').removeClass('active');
                $('.circle[data-n="' + i + '"]').addClass('active');
            };

            $('.circle-list').on('click', 'li', function(e) {
                if (e.target.nodeName == 'LI') {
                    i = Number($(e.target).attr('data-n'));
                    $(".hot").css('transform', 'translatex(' + (-i * width) + 'px)').css('transition', 'all 0.5s');
                    setCircle(i);
                }
            });

        });
    </script>
</body>

</html>